package com.skywork.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
public class EhCahceService {

	@Autowired
	private CacheManager cacheManager;

	public Object get(String cacheName, String key) {
		ValueWrapper valueWrapper = cacheManager.getCache(cacheName).get(key);
		if (valueWrapper != null) {
			return valueWrapper.get();
		}
		return null;
		// Cache cache = cacheManager.getCache(cacheName);
		// if (cache == null) {
		// cache = new Cache(cacheName, 1, false, false, 0L, 0L);
		// cacheManager.addCache(cache);
		// }
		// Element element = cache.get(key);
		// return ((element == null) ? null : element.getObjectValue());
	}

	public boolean put(String cacheName, String key, Object value) {
		boolean result = false;
		try {
			cacheManager.getCache(cacheName).put(key, value);
			// Cache cache = this.cacheManager.getCache(cacheName);
			// Element element = new Element(key, value);
			// cache.put(element);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}

	public boolean put(String cacheName, String key, Object value, Long expireTime) {
		return put(cacheName, key, value);
	}

	public void remove(String cacheName, String key) {
		cacheManager.getCache(cacheName).evict(key);
		// Cache cache = this.cacheManager.getCache(cacheName);
		// cache.remove(key);
	}

	public void setCache(String cacheName, String key, Object value, Long expiresIn) {
		put(cacheName, key, value);
	}

}
